
/**
 &i2c0 {
      i2c_demo: i2c_demo@39 {
          compatible = "i2c_demo";
          reg = <0x39>;
      };
 };
 */

#include <linux/module.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/string.h>
#include <linux/gpio.h>
#include <linux/interrupt.h>
#include <linux/i2c.h>
#include <linux/of.h>

#define COMPATILE_NAME "i2c_demo"


struct i2c_demo_priv {
	struct i2c_client *client;
};

static const struct i2c_device_id i2c_demo_id[] = {
	{ COMPATILE_NAME, 0 },
	{ },
};
MODULE_DEVICE_TABLE(i2c, i2c_demo_id);

#ifdef CONFIG_OF
static const struct of_device_id i2c_demo_of_table[] = {
	{ .compatible = COMPATILE_NAME },
	{ }
};
MODULE_DEVICE_TABLE(of, i2c_demo_of_table);
#endif

static int debug_enable = 0;
module_param(debug_enable,int, 0644);

static int i2c_demo_probe(struct i2c_client *client,
				   const struct i2c_device_id *id)
{
	struct device_node *node;
	struct i2c_demo_priv *chip;
	int ret, nr_port;
	dev_info(&client->dev, "%s enter\n", __func__);
	node = client->dev.of_node;
	chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
	if (chip == NULL)
		return -ENOMEM;
	chip->client = client;
	i2c_set_clientdata(client, chip);
	return 0;

}

static int i2c_demo_remove(struct i2c_client *client)
{
	int ret = 0;
	struct i2c_demo_priv *chip = i2c_get_clientdata(client);
	dev_info(&client->dev, "%s enter\n", __func__);
	return 0;
}

void i2c_demo_shutdown(struct i2c_client *client)
{
	int ret = 0;
	struct i2c_demo_priv *chip = i2c_get_clientdata(client);
	dev_info(&client->dev, "%s enter\n", __func__);
}

static struct i2c_driver i2c_demo_driver = {
	.driver = {
		.name		= "i2c_demo",
		.owner		= THIS_MODULE,
		.of_match_table	= of_match_ptr(i2c_demo_of_table),
	},
	.probe		= i2c_demo_probe,
	.remove		= i2c_demo_remove,
	.id_table	= i2c_demo_id,
	.shutdown = i2c_demo_shutdown,
};

static int __init i2c_demo_init(void)
{
	return i2c_add_driver(&i2c_demo_driver);
}
/* register after i2c postcore initcall and before
 * subsys initcalls that may rely on these GPIOs
 */
#if 1
subsys_initcall_sync(i2c_demo_init);
#else
module_init(i2c_demo_init);
#endif
static void __exit i2c_demo_exit(void)
{
	i2c_del_driver(&i2c_demo_driver);
}
module_exit(i2c_demo_exit);

MODULE_AUTHOR("tj <tj_wust@163.com>");
MODULE_DESCRIPTION("i2c demo driver");
MODULE_LICENSE("GPL");
